<%--
  Created by IntelliJ IDEA.
  User: LENOVO
  Date: 2021/6/4
  Time: 17:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <style type="text/css">
        .bg{
            padding-top: 200px;
            background-color: #99CC99;
            position: fixed;
            height: 1000px;
            width: 1536px;
        }
        .btn{
            margin-top: 30px;
        }
    </style>
</head>
<body>
<div class="bg" align="center">
    <div class="panel panel-success" style="width: 650px;border-radius: 20px;">
        <div class="panel-heading" style="border-radius: 20px 20px 0px 0px;">
            <h2 class="" style="font-weight: 600;">通用周报管理平台</h2>
        </div>
        <div class="panel-body" style="height: 300px;padding: 70px 50px;font-size: 20px;">
            <form class="form-horizontal" role="form" align="center" id="loginForm" action="/dailyreport">
                <div class="form-group">
                    <label class="col-sm-2 control-label">用户名</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="uname" placeholder="请输入用户名">
                    </div>
                </div>
                <div class="form-group">
                    <label for="upass" class="col-sm-2 control-label">密码</label>
                    <div class="col-sm-10">
                        <input type="password" class="form-control" id="upass" placeholder="请输入密码">
                    </div>
                </div>
                <input onclick="register()" type="button" class="btn btn-success btn-lg btn-block" value="注册"></input>
            </form>
        </div>
    </div>
    <script>
        function register(){
            var username=$("#uname").val();
            var password=$("#upass").val();
            console.log(username);
            if(username == "" || password == ""){
                alert("用户名和密码不能为空！");
                return;
            }
            $.ajax(
                {
                    url:"/dailyreport/user/checkuname",
                    data:{
                        "username":username
                    },
                    dataType:"json",
                    success:function (data) {
                        if(data.code=="1"){
                            alert("用户名已存在");
                        }
                        else{
                            saveUser();
                        }
                    },
                    error:function () {
                        alert("bad request");
                    }
                }
            )
        }
        function saveUser() {
            var username=$("#uname").val();
            var password=$("#upass").val();
            if(username == "" || password == ""){
                alert("用户名和密码不能为空！");
                return;
            }
            $.ajax({
                url:"/dailyreport/user/saveUser",
                data:{
                    "username":username,
                    "password":password
                },
                dataType:"json",
                success:function(data){
                    if(data.code == "1"){
                        alert("注册成功！");
                        window.location.href = "/dailyreport";
                    }
                    else{
                        alert("注册失败");
                    }
                }
            })
        }
    </script>
</div>
</body>
</html>
